/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName UnVersionedXMLHandler.as
 * @creationDate Aug 14, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.model.initialsetup.unversionedxmlhandlers.main
{
	import com.gesturesoft.imultimax.events.CustomErrorEvent;
	import com.gesturesoft.imultimax.events.CustomProgressEvent;
	import com.gesturesoft.imultimax.model.error.DataBaseError;
	import com.gesturesoft.imultimax.model.initialsetup.events.HandlerEvent;
	import com.gesturesoft.imultimax.model.initialsetup.unversionedxmlhandlers.handlers.AuthSettingsHandler;
	import com.gesturesoft.imultimax.model.initialsetup.unversionedxmlhandlers.handlers.CurrencyHandler;
	import com.gesturesoft.imultimax.model.initialsetup.interfaces.IHandler;
	import com.gesturesoft.imultimax.model.loader.CustomLoader;
	import com.gesturesoft.imultimax.model.loader.events.LoadFaultEvent;
	import com.gesturesoft.imultimax.model.loader.events.LoadResultEvent;
	import com.gesturesoft.imultimax.model.loader.interfaces.ILoader;
	
	import flash.events.EventDispatcher;
	
	import mx.managers.ICursorManager;

	public class UnVersionedXMLHandler extends EventDispatcher implements IHandler
	{
		public function UnVersionedXMLHandler()
		{
			
		}
		
		/**
		 * This is the function for download and update db with values, that need when app
		 * executes first time
		 * 
		 * @functionName start
		 * @creationDate Aug 16, 2012
		 * @author micmac
		 */
		public function start():void
		{
			
			//downlaodCurrency();
			//var currencyHandler:CurrencyHandler = new CurrencyHandler("http://www.micmacsolutions.com/gesture_soft/currency_codes.xml");
			downlaodCurrency();
			
		}
		
		/**
		 * This is the function for download currencies
		 * 
		 * @functionName downlaodCurrency
		 * @creationDate Aug 16, 2012
		 * @author micmac
		 * @params :
		 */
		private function downlaodCurrency():void
		{
			
			var iCurrencyLoader:ILoader = new CustomLoader("http://www.micmacsolutions.com/gesture_soft/currency_codes.xml");
			
			iCurrencyLoader.addEventListener(LoadResultEvent.LOAD_COMPLETE, onCurrencyLoadComplete);
			iCurrencyLoader.addEventListener(LoadFaultEvent.LOAD_FAILED,  onCurrencyLoadFailed);
			iCurrencyLoader.addEventListener(CustomProgressEvent.LOAD_PROGRESS, onCurrencyLoadProgress);
			
			iCurrencyLoader.load();
			
		}
		
		/**
		 * This is the function for description
		 * 
		 * @functionName onCurrencyLoadComplete
		 * @creationDate Aug 16, 2012
		 * @author micmac
		 * @params event:LoadResultEvent
		 */
		private function onCurrencyLoadComplete(event:LoadResultEvent):void
		{
			
			ILoader( event.target ).removeEventListener( LoadResultEvent.LOAD_COMPLETE, onCurrencyLoadComplete, true );
			ILoader( event.target ).removeEventListener( LoadFaultEvent.LOAD_FAILED, onCurrencyLoadFailed, true );
			ILoader( event.target ).removeEventListener( CustomProgressEvent.LOAD_PROGRESS, onCurrencyLoadProgress, true );
			
			var currencyHandler:CurrencyHandler = new CurrencyHandler();
			
			try
			{
				
				currencyHandler.insertCurrency(event);
				
			} 
			catch(error:DataBaseError) 
			{
				
				trace("error in : InitialSetupHandler:onCurrencyLoadComplete");
				throw error;
				
			}
			
			downloadAuthSettings();
			
		}
		
		/**
		 * This is the function for description
		 * 
		 * @functionName onCurrencyLoadFailed
		 * @creationDate Aug 16, 2012
		 * @author micmac
		 * @params event:LoadFaultEvent
		 */
		private function onCurrencyLoadFailed(event:LoadFaultEvent):void
		{
			
			ILoader( event.target ).removeEventListener( LoadResultEvent.LOAD_COMPLETE, onCurrencyLoadComplete, true );
			ILoader( event.target ).removeEventListener( LoadFaultEvent.LOAD_FAILED, onCurrencyLoadFailed, true );
			ILoader( event.target ).removeEventListener( CustomProgressEvent.LOAD_PROGRESS, onCurrencyLoadProgress , true );
			
			dispatchEvent(new CustomErrorEvent(CustomErrorEvent.ERROR_OCCURED, "Currency load failed"));
			
		}
		
		/**
		 * This is the function for handle the progress of downloading
		 * 
		 * @functionName onLoadProgress
		 * @creationDate Aug 16, 2012
		 * @author micmac
		 * @params event:CustomProgressEvent
		 */
		private function onCurrencyLoadProgress(event:CustomProgressEvent):void
		{
			
			dispatchEvent( new CustomProgressEvent( CustomProgressEvent.CURRENCY_LOAD_PROGRESS, event.bytesLoaded, event.bytesTotal ) );
			//dispatchEvent( event.clone() );
			
		}
		
		//-------------------------------Currency load and update with db complte-----------------------------------
		
		/**
		 * This is the function for download authentication settings
		 * 
		 * @functionName fname
		 * @creationDate Aug 16, 2012
		 * @author micmac
		 * @params arg:String
		 */
		private function downloadAuthSettings():void
		{
			
			var iAuthSettingsLoader:ILoader = new CustomLoader("http://www.micmacsolutions.com/gesture_soft/settings.xml");
			
			iAuthSettingsLoader.addEventListener(LoadResultEvent.LOAD_COMPLETE, onAuthSettingsLoadComplete);
			iAuthSettingsLoader.addEventListener(LoadFaultEvent.LOAD_FAILED,  onAuthSettingsLoadFailed);
			iAuthSettingsLoader.addEventListener(CustomProgressEvent.LOAD_PROGRESS, onAuthSettingsLoadProgress);
			
			iAuthSettingsLoader.load();
			
		}
		
		/**
		 * This is the function for description
		 * 
		 * @functionName onCurrencyLoadComplete
		 * @creationDate Aug 16, 2012
		 * @author micmac
		 * @params event:LoadResultEvent
		 */
		private function onAuthSettingsLoadComplete(event:LoadResultEvent):void
		{
			
			ILoader( event.target ).removeEventListener( LoadResultEvent.LOAD_COMPLETE, onAuthSettingsLoadComplete, true );
			ILoader( event.target ).removeEventListener( LoadFaultEvent.LOAD_FAILED, onAuthSettingsLoadFailed, true );
			ILoader( event.target ).removeEventListener( CustomProgressEvent.LOAD_PROGRESS, onAuthSettingsLoadProgress, true );
			
			var authSettingsHandler:AuthSettingsHandler = new AuthSettingsHandler();
			
			try
			{
				
				authSettingsHandler.insertAuthSettings(event);
				
			} 
			catch(error:DataBaseError) 
			{
				
				throw error;
				return;
				
			}
			
			dispatchEvent(new HandlerEvent(HandlerEvent.HANDLE_COMPLETE));
			
		}
		
		/**
		 * This is the function for description
		 * 
		 * @functionName onCurrencyLoadFailed
		 * @creationDate Aug 16, 2012
		 * @author micmac
		 * @params event:LoadFaultEvent
		 */
		private function onAuthSettingsLoadFailed(event:LoadFaultEvent):void
		{
			
			ILoader( event.target ).removeEventListener( LoadResultEvent.LOAD_COMPLETE, onAuthSettingsLoadComplete, true );
			ILoader( event.target ).removeEventListener( LoadFaultEvent.LOAD_FAILED, onAuthSettingsLoadFailed, true );
			ILoader( event.target ).removeEventListener( CustomProgressEvent.LOAD_PROGRESS, onAuthSettingsLoadProgress, true );
			
			dispatchEvent(new CustomErrorEvent(CustomErrorEvent.ERROR_OCCURED, "Currency load failed"));
			
		}
		
		/**
		 * This is the function for handle the progress of downloading
		 * 
		 * @functionName onLoadProgress
		 * @creationDate Aug 16, 2012
		 * @author micmac
		 * @params event:CustomProgressEvent
		 */
		private function onAuthSettingsLoadProgress(event:CustomProgressEvent):void
		{
			
			dispatchEvent( new CustomProgressEvent( CustomProgressEvent.AUTH_SETTINGS_LOAD_PROGRESS, event.bytesLoaded, event.bytesTotal ) );
			
		}
		
		//-------------------------------------------------------------------------------------------------
		
		
	}
}